home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / adir2cnc.zip / ADI2CNC.BAS next >
BASIC Source File  |  1990-11-27  |  11KB  |  495 lines

  1. ' quick basic program to convert apple plotter commands
  2. ' from autocad adi plotter file into G codes for use with
  3. ' CNC milling machine
  4. '
  5. ' written by george mcduffee at allen county community college
  6. ' march 13th, 1990
  7. ' and released to the public domain.
  8. '
  9. ' program recognizes the following however other ADI commands can be added
  10. ' if required.
  11. '
  12. ' 2 = home
  13. ' G90
  14. ' G00/000/000/000
  15. '
  16. ' 3 = move absolute [pen up]
  17. ' G91
  18. ' G01 000/000/0010
  19. ' G90
  20. ' G00 X/Y/Z
  21. ' G91
  22. ' G00 000/000/-010
  23. '
  24. ' 4 = plot absolute [pen down]
  25. ' G91 X/Y/Z
  26. '
  27. ' exact conversion values will depend on how adi plotter selection
  28. ' is set up.  This program assumes 254 points to the inch horz and vert.
  29. '
  30. ' Plot limits assumed to be within CNC limits - 7.87 in [200 m/m] X
  31. ' 3.94 in [100 m/m] for Emco F1-CNC machine.
  32. '
  33. ' -------- start of program -------------
  34.  
  35. OPTION BASE 1
  36.  
  37. DEFINT A-Z
  38.  
  39. CLS
  40. LOCATE 10, 10
  41. PRINT "This program will convert a AutoCadd ADI plotter output file"
  42. LOCATE 11, 10
  43. PRINT "into CNC G codes.  It assumes plot done at 1[inch] = 1[inch] and"
  44. LOCATE 12, 10
  45. PRINT "plot size limited to 7.87 inches in the X direction and 3.94 inches"
  46. LOCATE 13, 10
  47. PRINT "in the Y direction and that only ADI commands 2/3/4 [home/move abs/"
  48. LOCATE 14, 10
  49. PRINT "plot abs are used.  Output will be written to <INFILE>.CNC"
  50. LOCATE 16, 10
  51. INPUT "input name of plot file"; plotfile$
  52.  
  53. ' ---------------- OPEN FILES --------------------------
  54. plotfile$ = LTRIM$(RTRIM$(plotfile$))
  55.  
  56. temp = INSTR(plotfile$, ".")
  57.  
  58. IF (temp = 0) THEN
  59.      plotfile$ = plotfile$ + ".plt"
  60. END IF
  61.  
  62. OPEN plotfile$ FOR INPUT ACCESS READ AS #1 LEN = 16284
  63.  
  64. temp = INSTR(plotfile$, ".")
  65.  
  66. IF temp = 0 THEN
  67.      gcodefile$ = plotfile$ + ".cnc"
  68. ELSE
  69.      gcodefile$ = LEFT$(plotfile$, (temp - 1))
  70.      gcodefile$ = gcodefile$ + ".cnc"
  71. END IF
  72.  
  73. OPEN gcodefile$ FOR OUTPUT ACCESS WRITE AS #2 LEN = 16284
  74.  
  75.  
  76. ' ----------------- DIM VARIABLES -----------------------
  77. DIM comma(4) AS INTEGER
  78. DIM InLineCounter AS INTEGER
  79.  
  80. ' Emco F1 omits decimal point
  81. ' i.e. "0050" is  0.050
  82. CONST clearence = "   150"
  83.  
  84. ' feed is in 1/10 inches per min.
  85. ' i.e. "020" = 2 inches per min.
  86. CONST Feed = "  20"
  87.  
  88. ' factor to convert ADI values to CNC values
  89. ' in this case 0.1 M/M to .001 inches
  90. CONST ConversionFactor# = .254
  91.  
  92. ' emco F1-CNC controller requires information in specific column format
  93.  
  94. DIM HeaderLine1 AS STRING * 4
  95.  HeaderLine1 = CHR$(255) + CHR$(255) + CHR$(255) + "%"
  96.  
  97. DIM HeaderLine2 AS STRING * 30
  98.  HeaderLine2 = "    N` G`  X  `  Y `   Z ` F  "
  99.  
  100. DIM LastLine AS STRING
  101. LastLine = "  " + CHR$(34) + "I":  'this is >   "I< indicating inch Vertical
  102. LastLine = LastLine + CHR$(16) + CHR$(16) + CHR$(16) + CHR$(16) + CHR$(16)
  103. LastLine = LastLine + CHR$(16) + CHR$(16) + CHR$(16) + CHR$(16) + CHR$(16)
  104.  
  105. CONST FeedRate = "  10"
  106.  
  107. TYPE EmcoLine
  108.      filler AS STRING * 3
  109.      BlockNo AS STRING * 3
  110.      Gcode AS STRING * 3
  111.      CNCx AS STRING * 6
  112.      CNCy AS STRING * 5
  113.      CNCz AS STRING * 6
  114.      Feed AS STRING * 4
  115. END TYPE
  116.  
  117. DIM emco AS EmcoLine
  118. emco.filler = "   "
  119. emco.BlockNo = "000"
  120. emco.Gcode = " 21"
  121. emco.CNCx = "    00"
  122. emco.CNCy = "   00"
  123. emco.CNCz = "    00"
  124. emco.Feed = "    "
  125.  
  126.  
  127. DIM blankemco AS EmcoLine
  128.     blankemco.filler = "   "
  129.     blankemco.Gcode = "   "
  130.     blankemco.CNCx = "      "
  131.     blankemco.CNCy = "     "
  132.     blankemco.CNCz = "      "
  133.     blankemco.Feed = "    "
  134.  
  135. emco = blankemco
  136.  
  137. DIM OldCNCx AS STRING * 6
  138. OldCNCx = "    00"
  139.  
  140. DIM OldCNCy AS STRING * 5
  141. OldCNCy = "   00"
  142.  
  143. DIM OldCNCz AS STRING * 6
  144. OldCNCz = "    00"
  145.  
  146. DIM blank6 AS STRING * 6
  147.      blank6 = "      "
  148. DIM blank5 AS STRING * 5
  149.      blank5 = "     "
  150. DIM blank4 AS STRING * 4
  151.      blank4 = "    "
  152. DIM blank3 AS STRING * 3
  153.      blank3 = "   "
  154.  
  155. DIM tempd AS DOUBLE
  156.  
  157. DIM pc, px, py AS DOUBLE
  158.  
  159. TYPE emcostring
  160.      es1 AS STRING * 30
  161. END TYPE
  162.  
  163. DIM es AS emcostring
  164.  
  165.  
  166. ' ----------------- START OF MAIN -----------------------
  167.  
  168. ' send header lines to cnc file
  169. PRINT #2, HeaderLine1
  170. PRINT #2, HeaderLine2
  171.  
  172.  
  173. linecount = -1
  174.  
  175. ' set cnc to absolute mode
  176.  
  177. emco = blankemco
  178.  
  179. GOSUB MakeNstring
  180.  
  181. emco.BlockNo = Nstring$
  182. emco.Gcode = " 90"
  183. PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  184.  
  185.  
  186. WHILE NOT (EOF(1))
  187. LINE INPUT #1, PlotterLine$
  188.  
  189. ' for debug
  190. ' PRINT PlotterLine$
  191.  
  192. GOSUB Convert2CNC
  193. WEND
  194.  
  195. ' write M30 as last line of cnc file
  196.  
  197.      GOSUB MakeNstring
  198.      emco = blankemco
  199.      emco.BlockNo = Nstring$
  200.      emco.Gcode = "M30":  'rapid traverse
  201.      emco.CNCx = "      "
  202.      emco.CNCy = "     "
  203.      emco.CNCz = "      "
  204.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  205.  
  206. ' add lst line
  207.      PRINT #2, LastLine
  208. ' PRINT #2, ""
  209.  
  210.  
  211. CLOSE
  212.  
  213. ' display warning if block count greater than F1-CNC limits
  214. IF linecount > 221 THEN
  215.      CLS
  216.      LOCATE 10, 10
  217.      PRINT "WARNING"
  218.      LOCATE 12, 10
  219.      PRINT "Generated program contains "; linecount; " blocks"
  220.      LOCATE 14, 10
  221.      PRINT "The limit of the EMCO controller is 221."
  222.      LOCATE 16, 10
  223.      PRINT "DATA is usable -- edit and run as two programs"
  224. END IF
  225.  
  226.  
  227. END
  228.  
  229. ' -------------------- END OF MAIN -----------------------
  230.  
  231. ' -----------
  232. Convert2CNC:
  233.  
  234.  
  235. ' set px and py [plotter x and plotter y to -1
  236. ' if -1 shows these were not set and error if not 'home'
  237. pc = -1
  238. px = -1
  239. py = -1
  240.  
  241. ' parse plotter line and check data format
  242. 'count the number of commas in plotter line assume 2 max but check for
  243. ' three for error condition
  244. comma(1) = 0
  245. comma(2) = 0
  246. comma(3) = 0
  247.  
  248. 'increment plotter input file line counter for error reporting
  249. InLineCounter = InLineCounter + 1
  250.  
  251. comma(1) = INSTR(PlotterLine$, ",")
  252.  
  253. IF comma(1) > 0 THEN
  254. comma(2) = INSTR(comma(1) + 1, PlotterLine$, ",")
  255. END IF
  256.  
  257. IF comma(2) > 0 THEN
  258. comma(3) = INSTR(comma(2) + 1, PlotterLine$, ",")
  259. END IF
  260.  
  261. ' for debug
  262. ' PRINT "In Line = "; InLineCounter
  263. ' PRINT "comma(1) = "; comma(1)
  264. ' PRINT "comma(2) = "; comma(2)
  265. ' PRINT "comma(3) = "; comma(3)
  266.  
  267. IF comma(3) > 0 THEN
  268. CLS
  269. LOCATE 5, 10
  270. PRINT "error condition in input file at line "; InLineCounter
  271. LOCATE 7, 10
  272. PRINT "More than 2 commas found. "
  273. LOCATE 9, 10
  274. PRINT "correct input [plotter] file and rerun.  Press [Enter] key to exit."
  275. INPUT ; "", dummy$
  276. END
  277. END IF
  278.  
  279. IF comma(1) = 0 THEN
  280. pc = VAL(PlotterLine$)
  281. ELSE
  282. IF comma(2) = 0 THEN
  283. 'error as line must have no or two commas
  284. CLS
  285. LOCATE 5, 10
  286. PRINT "error condition in input file at line "; InLineCounter
  287. LOCATE 7, 10
  288. PRINT "Only 1 comma found. Line must have no or two commas"
  289. LOCATE 9, 10
  290. PRINT "correct input [plotter] file and rerun.  Press [Enter] key to exit."
  291. INPUT ; "", dummy$
  292. END
  293. END IF
  294.  
  295. pc = VAL(PlotterLine$)
  296. temp$ = MID$(PlotterLine$, comma(1) + 1, (comma(2) - comma(1) - 1))
  297. px = VAL(temp$)
  298. temp$ = MID$(PlotterLine$, comma(2) + 1)
  299. py = VAL(temp$)
  300. END IF
  301.  
  302. ' debug
  303. ' PRINT "pc ="; pc
  304. ' PRINT "px ="; px
  305. ' PRINT "py ="; py
  306.  
  307. GOSUB WriteGCode
  308.  
  309.  
  310. RETURN
  311. ' -----------
  312.  
  313. ' -----------
  314. WriteGCode:
  315. ' px and py from plotter file are in .1 M/M
  316.  
  317. ' save existing CNC x/y/z  values for possible later use to withdraw cutter
  318. ' without setting to relative mode and then back [saves blocks]
  319. IF NOT emco.CNCx = "      " THEN
  320.      OldCNCx = emco.CNCx
  321. END IF
  322.  
  323. IF NOT emco.CNCy = "     " THEN
  324.      OldCNCy = emco.CNCy
  325. END IF
  326.  
  327. IF NOT emco.CNCz = "      " THEN
  328.      OldCNCz = emco.CNCz
  329. END IF
  330.  
  331.  
  332. ' move absolute pen up
  333. IF (pc = 3) THEN GOSUB MoveCutterUp
  334.  
  335. 'move absolute pen down
  336. IF pc = 4 THEN GOSUB MillLine
  337.  
  338. ' home
  339. IF (pc = 2 AND px = -1 AND py = -1) THEN GOSUB Home
  340.  
  341. RETURN
  342. ' --------------
  343.  
  344. ' -------------
  345. MakeNstring:
  346.  
  347. linecount = linecount + 1
  348.  
  349. n$ = STR$(linecount)
  350. n$ = RTRIM$(LTRIM$(n$))
  351. SELECT CASE LEN(n$)
  352. CASE IS = 1
  353.      Nstring$ = "00" + n$
  354. CASE IS = 2
  355.      Nstring$ = "0" + n$
  356. CASE IS = 3
  357.      Nstring$ = n$
  358. CASE ELSE
  359. END SELECT
  360.  
  361. RETURN
  362. ' ----------
  363.  
  364. ' ---------- homes the head
  365. Home:
  366.  
  367.      'withdraw cutter under rapid traverse
  368.      GOSUB MakeNstring
  369.      emco = blankemco
  370.      emco.BlockNo = Nstring$
  371.      emco.Gcode = " 00":  'rapid traverse
  372.      emco.CNCx = OldCNCx
  373.      emco.CNCy = OldCNCy
  374.      emco.CNCz = "    00"
  375.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  376.  
  377.      ' move to home under rapid traverse
  378.      GOSUB MakeNstring
  379.      emco = blankemco
  380.      emco.BlockNo = Nstring$
  381.      emco.Gcode = " 00":  'rapid traverse
  382.      emco.CNCx = "    00"
  383.      emco.CNCy = "   00"
  384.      emco.CNCz = "    00"
  385.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  386.  
  387.  
  388. RETURN
  389. ' -----------
  390.  
  391. ' ----------
  392. MoveCutterUp:
  393.  
  394.      ' withdraw cutter at rapid traverse
  395.      GOSUB MakeNstring
  396.      emco = blankemco
  397.      emco.BlockNo = Nstring$
  398.      emco.Gcode = " 00":  'rapid traverse
  399.      emco.CNCx = OldCNCx
  400.      emco.CNCy = OldCNCy
  401.      emco.CNCz = "    00"
  402.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  403.  
  404.  
  405.      ' move to new location at rapid traverse
  406.      GOSUB MakeNstring
  407.      emco = blankemco
  408.      GOSUB ConvertP2G
  409.      emco.BlockNo = Nstring$
  410.      emco.Gcode = " 00":  'rapid traverse
  411.      emco.CNCz = "    00"
  412.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  413.  
  414.  
  415.      ' bring cutter down at proper feed rate at new location
  416.      GOSUB MakeNstring
  417.      emco.BlockNo = Nstring$
  418.      emco.Gcode = " 01":  'linear interpolation
  419.      emco.CNCz = clearence
  420.      emco.Feed = FeedRate
  421.      MID$(emco.CNCz, 1) = "-": '- to feed down
  422.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  423.  
  424.  
  425.  
  426.  
  427. RETURN
  428. ' ----------
  429.  
  430. ' ----------
  431. MillLine:
  432.      ' mill line
  433.      GOSUB MakeNstring
  434.      emco = blankemco
  435.      GOSUB ConvertP2G
  436.      emco.CNCz = clearence
  437.      MID$(emco.CNCz, 1, 1) = "-"
  438.      emco.BlockNo = Nstring$
  439.      emco.Gcode = " 01":  'linear interp
  440.      emco.Feed = FeedRate
  441.      PRINT #2, emco.filler; emco.BlockNo; emco.Gcode; emco.CNCx; emco.CNCy; emco.CNCz; emco.Feed
  442.  
  443.  
  444.     
  445. RETURN
  446. ' ----------
  447.  
  448. ConvertP2G:
  449. ' converts adi plotter values to 1000's of inch for F1 controller
  450.  
  451. OldCNCx = emco.CNCx
  452. OldCNCy = emco.CNCy
  453.  
  454.  
  455.      tempd = (px / ConversionFactor)
  456.      tempI = INT(tempd)
  457.      temp$ = ""
  458.      temp$ = STR$(tempI)
  459.      temp$ = RTRIM$(LTRIM$(temp$))
  460.      SELECT CASE LEN(temp$)
  461.           CASE IS = 1
  462.                temp$ = "     " + temp$
  463.           CASE IS = 2
  464.                temp$ = "    " + temp$
  465.           CASE IS = 3
  466.                temp$ = "   " + temp$
  467.           CASE 4
  468.                temp$ = "  " + temp$
  469.           CASE ELSE
  470.                temp$ = "~~~~~~"
  471.      END SELECT
  472.      emco.CNCx = temp$
  473.  
  474.      tempd = (py / ConversionFactor)
  475.      tempI = INT(tempd)
  476.      temp$ = ""
  477.      temp$ = STR$(tempI)
  478.      temp$ = RTRIM$(LTRIM$(temp$))
  479.      SELECT CASE LEN(temp$)
  480.           CASE IS = 1
  481.                temp$ = "    " + temp$
  482.           CASE IS = 2
  483.                temp$ = "   " + temp$
  484.           CASE IS = 3
  485.                temp$ = "  " + temp$
  486.           CASE 4
  487.                temp$ = " " + temp$
  488.           CASE ELSE
  489.                temp$ = "~~~~~"
  490.      END SELECT
  491.      emco.CNCy = temp$
  492.  
  493. RETURN
  494.  
  495.